package com.example.common;


import jakarta.annotation.Resource;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Resource
    JwtInterceptor jwtInterceptor;

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer){
        //指定Controller统一的接口前缀
        configurer.addPathPrefix("/api",clazz -> clazz.isAnnotationPresent(RestController.class));
    }

    //添加自定义拦截器JwtInterceptor 设置拦截规则
    @Override
    public void addInterceptors(InterceptorRegistry registry){

        registry.addInterceptor(jwtInterceptor).addPathPatterns("/api/**")
                .excludePathPatterns("/api/user/login")
                .excludePathPatterns("/api/user/register")
                .excludePathPatterns("/api/user/registershopper/**")
                .excludePathPatterns("/api/user/sendmailcode/**")
                .excludePathPatterns("/api/user/checkcode/**")
                .excludePathPatterns("/api/user/resetpwd")
                .excludePathPatterns("/api/files/**");

    }
}
